package com.pj.wisdomlogistics.ui.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.pj.wisdomlogistics.MyApplication;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.TransactHistoryInfo;
import com.pj.wisdomlogistics.mvp.contract.TransactHistoryContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.TransactHistoryPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.TransactHistoryAdapter;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CustomToast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class TransactHistoryActivity extends MvpActivity<TransactHistoryPresenter> implements TransactHistoryContract.View, TransactHistoryAdapter.onItemClick, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.rv_history)
    RecyclerView mRvHistory;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mRefreshLayout;

    private String oldDate;
    private String currentDate;
    private TransactHistoryAdapter mAdapter;
    public SimpleDateFormat mSf = new SimpleDateFormat("yyyy-MM-dd");
    private boolean isRefresh = false;
    private boolean isReSet = false;

    @Override
    public void initView() {
        mRvHistory.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TransactHistoryAdapter(this);
        mRvHistory.setAdapter(mAdapter);
        mAdapter.setListener(this);

        mRefreshLayout.setColorSchemeResources(
                R.color.google_orange,
                R.color.google_yellow,
                R.color.google_blue,
                R.color.google_green
        );

        mRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void initData() {
        currentDate = mSf.format(new Date(System.currentTimeMillis()));
        oldDate = currentDate;
        mPresenter.alreadydo(MyApplication.userID, currentDate);
        showProgressDialog();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_transact_history;
    }

    @Override
    public boolean isApplyEventBus() {
        return true;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == Cons.Date.DATE) {
            currentDate = (String) eventCenter.getData();
            mPresenter.alreadydo(MyApplication.userID, currentDate);
            showProgressDialog();
            isReSet = true;
        }
    }

    @Override
    public void onError(String errorMsg) {
        dismissProgressDialog();
        CustomToast.showToast(this, errorMsg, 10000);
        currentDate = oldDate;
    }

    @Override
    public void onSuccess(TransactHistoryInfo mInfo) {
        dismissProgressDialog();
        oldDate = currentDate;

        if (mInfo.getList() == null) return;

        if (isRefresh && mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        mAdapter.setList(mInfo.getList());
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }


    @OnClick({R.id.iv_back, R.id.tv_past, R.id.tv_feture})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
//            case R.id.iv_date:
//                CalendarUtil.create().getCalendarDate(this);
//                break;
            case R.id.tv_past:
                currentDate = getDate(true, currentDate);
                mPresenter.alreadydo(MyApplication.userID, currentDate);
                showProgressDialog();
                break;
            case R.id.tv_feture:
                currentDate = getDate(false, currentDate);
                mPresenter.alreadydo(MyApplication.userID, currentDate);
                showProgressDialog();
                break;
        }
    }

    @Override
    public void itemClick(int type, int id) {
        Intent mIntent = null;
        switch (type) {
            case Cons.ApplyType.APPLY_GWYC:
                mIntent = new Intent(this, CarDetailActivity.class);
                mIntent.putExtra(Cons.CarInfo.CAR_TRANSACT_HISTROY_ID, id);
                break;
            case Cons.ApplyType.APPLY_HYS:
                mIntent = new Intent(this, ApplyDetailActivity.class);
                mIntent.putExtra(Cons.TransactDetail.MEETING_TRANSACT_ID, id);
                break;
            case Cons.ApplyType.APPLY_ST:
                mIntent = new Intent(this, MealDetialActivity.class);
                mIntent.putExtra(Cons.MealInfo.MEAL_RANSACT_ID, id);
                break;
            case Cons.ApplyType.APPLY_WHBX:
                mIntent = new Intent(this, RepairDetailActivity.class);
                mIntent.putExtra(Cons.RepairInfo.REPAIR_RANSACT_ID, id);
                break;
            case Cons.ApplyType.APPLY_WMDC:
                break;
        }
        mIntent.putExtra(Cons.ApplyDetail.MEETING_ID, id);
        startActivity(mIntent);
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        currentDate = mSf.format(new Date(System.currentTimeMillis()));
        mPresenter.alreadydo(MyApplication.userID, currentDate);
        showProgressDialog();
    }

    /**
     * isPast:true 上一周 false 下一周
     */
    public String getDate(boolean isPast, String mDate) {
        String result = "";
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(mSf.parse(mDate));
            calendar.set(Calendar.DAY_OF_YEAR, isPast ? calendar.get(Calendar.DAY_OF_YEAR) - 7 : calendar.get(Calendar.DAY_OF_YEAR) + 7);
            Date today = calendar.getTime();
            result = mSf.format(today);
        } catch (ParseException mE) {
            mE.printStackTrace();
        }
        return result;
    }
}
